package com.betterjr.modules.application;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.betterjr.common.web.ControllerExceptionHandler;
import com.betterjr.common.web.ControllerExceptionHandler.ExceptionHandler;
import com.betterjr.common.web.Servlets;

/**
 * 
 * @ClassName: AmsMenuInfoController 
 * @Description: 菜单servlet入口 
 * @author xuyp
 * @date 2017年11月21日 上午9:14:30 
 *
 */
@Controller
@RequestMapping(value = "/Platform/AmsMenuInfo")
public class AmsMenuInfoController {

    private static final Logger logger = LoggerFactory.getLogger(AmsMenuInfoController.class);

    @Reference(interfaceClass = IAmsMenuInfoService.class)
    private IAmsMenuInfoService menuInfoService;

    /**
     * 
     * @Title: saveAddMenuInfo 
     * @Description: 菜单新增
     * @param request
     * @param parentId 父菜单id
     * @param menuName 菜单名称
     * @param applicationId 应用id
     * @return 新增操作结果字符串
     * @throws 
     * @author xuyp
     * @date 2017年11月21日 上午9:13:34
     */
    @RequestMapping(value = "/saveAddMenuInfo", method = RequestMethod.POST)
    public @ResponseBody String saveAddMenuInfo(final HttpServletRequest request, Long parentId, String menuName,
            Long applicationId) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return menuInfoService.webSaveAddMenuInfo(parentId, menuName, applicationId);
            }
        }, "操作失败", logger);
    }

    /**
     * 
     * @Title: saveUpdateMenuInfo 
     * @Description: 菜单更新 
     * @param request
     * @param resourceIds 资源id集合
     * @return 菜单更新操作结果（String）
     * @throws 
     * @author xuyp
     * @date 2017年11月21日 上午9:16:07
     * menuName
     * menuTitle
     * description
     * url
     * targetForm
     * applicationId
     * ruleList
     * parentId
     * endNode
     */
    @RequestMapping(value = "/saveUpdateMenuInfo", method = RequestMethod.POST)
    public @ResponseBody String saveUpdateMenuInfo(final HttpServletRequest request, String resourceIds) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return menuInfoService.webSaveUpdateMenuInfo(anMap, resourceIds);
            }
        }, "操作失败", logger);
    }

    /**
     * 
     * @Title: findMenuInfoDetail 
     * @Description: 查询菜单详细信息 
     * @param request
     * @param id 菜单id
     *        resourceModelType 1 返回资源封装后的信息
     * @return 返回菜单查询结果
     * @throws 
     * @author xuyp
     * @date 2017年11月21日 上午9:18:18
     */
    @RequestMapping(value = "/findMenuInfoDetail", method = RequestMethod.POST)
    public @ResponseBody String findMenuInfoDetail(final HttpServletRequest request, Long id,
            String resourceModelType) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return menuInfoService.webFindMenuInfoDetail(id, resourceModelType);
            }
        }, "操作失败", logger);
    }

    /**
     * 
     * @Title: selectMenuListByApplicaitonid 
     * @Description:  通过应用查询菜单信息
     * @param request
     * @param applicationId 应用id
     * @param flag 是否查询资源信息  1 查询    9 不查询
     * @return 当前应用下所有的菜单信息
     * @throws 
     * @author xuyp
     * @date 2017年11月21日 上午9:21:19
     */
    @RequestMapping(value = "/selectMenuListByApplicaitonid", method = RequestMethod.POST)
    public @ResponseBody String selectMenuListByApplicaitonid(final HttpServletRequest request, Long applicationId,
            String flag) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return menuInfoService.webSelectMenuListByApplicaitonid(applicationId, flag);
            }
        }, "操作失败", logger);
    }

    /**
     * 
     * @Title: saveDeleteMenuInfoById 
     * @Description: 菜单下架
     * @param request
     * @param id 菜单id
     * @return 菜单下架 
     * @throws 
     * @author xuyp
     * @date 2017年12月21日 下午3:24:28
     */
    @RequestMapping(value = "/saveDeleteMenuInfoById", method = RequestMethod.POST)
    public @ResponseBody String saveDeleteMenuInfoById(final HttpServletRequest request, Long id) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return menuInfoService.webSaveDeleteMenuInfoById(id);
            }
        }, "操作失败", logger);
    }

}
